package com.itheima.controller;


import com.itheima.common.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.OrderSettingVO;
import com.itheima.service.IOrdersettingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.xmlbeans.impl.common.IOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Locale;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-12-21
 */
@RestController
@RequestMapping("/ordersetting")
@Slf4j
@Api(tags = "订单设置")
public class OrdersettingController {
    @Autowired
    private IOrdersettingService ordersettingService;

    @GetMapping("/getOrderSettingByMonth")
    @ApiOperation("获取预约设置数据")
    public Result getOrderSettingByMonth(@RequestParam String month){
        List<OrderSettingVO> list = ordersettingService.getOrderSettingByMonth(month);
        return new Result(true,"获取预约设置数据成功",list);
    }
    @PostMapping("/editNumberByOrderDate")
    @ApiOperation("编辑预约设置数据")
    public Result editNumberByOrderDate(@RequestBody OrderSetting orderSetting){
        ordersettingService.createOrUpdate(orderSetting);
        return new Result(true,"编辑预约设置数据成功");
    }

    /**
     * 文件下载
     * @param filename
     * @param response
     * @return
     * @throws Exception
     */
    @GetMapping("/download")
    public void download(@RequestParam String filename, HttpServletResponse response) throws Exception {
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("templates" + "/" + filename);
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
        byte[] bytes = IOUtils.toByteArray(inputStream);
        // 将文件内容写入响应输出流
        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write(bytes);
        outputStream.flush();
        // 关闭输入流
        inputStream.close();
    }
    @PostMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile multipartFile) throws Exception {
        FileInputStream fileInputStream =(FileInputStream) multipartFile.getInputStream();
        XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);
        XSSFSheet sheet = workbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        for (int i = 1; i <= lastRowNum; i++) {
            OrderSetting orderSetting = new OrderSetting();
            orderSetting.setNumber(Integer.parseInt(sheet.getRow(i).getCell(2).getStringCellValue()));
            orderSetting.setOrderDate(sheet.getRow(i).getCell(1).getDateCellValue());
            ordersettingService.createOrUpdate(orderSetting);
        }
        return new Result(true,"上传成功");
    }
}
